<?php
/**
 * User: Jerry
 * Date: 2017/7/24
 * Time: 17:00
 */

namespace SDXApi\Service;


use SDXApi\Abstracts\TemplateAbstract;
use SDXApi\Model\HomeTemplateVogueModel;
use SDXApi\Repositories\GoodsRepository;

/**
 * Class HomeTemplateVogueService
 * @author Jerry
 * @package SDXApi\Service
 */
class HomeTemplateVogueService extends TemplateAbstract
{
    /**
     * @var HomeTemplateVogueModel
     */
    protected $vogueModel;

    /**
     * @var GoodsRepository
     */
    protected $goodsRepository;

    /**
     * @var RecommendNewService
     */
    protected $recommendNewService;

    /**
     * HomeTemplateVogueService constructor.
     */
    public function __construct()
    {
        $this->vogueModel = new HomeTemplateVogueModel();
        $this->goodsRepository = new GoodsRepository();
        $this->recommendNewService = new RecommendNewService();
    }

    /**
     * @author Jerry
     * @param array $template
     * @param int $userId
     * @param int $wid
     * @return array
     */
    public function handle($template = array(), $userId = 0, $wid = 8)
    {
        return $this->handleGoods($template, $userId, $wid);
    }

    /**
     * @author Jerry
     * @param array $template
     * @param int $userId
     * @param int $wid
     * @return array
     */
    protected function handleGoods($template = array(), $userId = 0, $wid = 8)
    {

        $goods = $this->getGoods($template, $wid, $userId);

        if (empty($goods)) {
            $goods = array();
        }

        if ($template['orientation'] == 0) {
            $template['orientation'] = 1;
        }

        $template['vogue_data'] = $goods;
        $template['vogue_link'] = $template['more_link'];
        unset($template['more_link']);

        return $template;
    }

    /**
     * @author Jerry
     * @param array $template
     * @param int $wid
     * @param int $userId
     * @return mixed
     */
    protected function getGoods($template = array(), $wid = 8, $userId = 0)
    {

        $limit = $this->getVogueLimit($template['orientation']);

        $goods = $this->getLimitGoods($userId, $wid, $limit, $template);
//        dd($goods);
        if (! empty($goods)) {
            $goodsData = $this->goodsRepository->formatGoodsTransform($goods);
            $goods = $this->checkDataSort($goodsData, 'goods_number');
        }

        return $goods;
    }

    /**
     * @author Jerry
     * @param int $userId
     * @param int $wid
     * @param int $limit
     * @param array $template
     * @return mixed
     */
    protected function getLimitGoods($userId = 0, $wid = 8, $limit = 0, $template = array())
    {
        $cacheName = 'home_template_vogue_goods_uid_' . $userId;

        $goodsList = $this->getCache($cacheName);

        if (empty($goodsList)) {
            $goodsIds = $this->recommendNewService->getHomeVogueData($userId, array(), 100);
            $goodsList = $this->vogueModel->getVogueGoodsByIds($goodsIds, $wid);
            $goodsList = $this->goodsRepository->filterGoodsNumber($goodsList);
            shuffle($goodsList);
            $goodsList = array_chunk($goodsList, $limit);
        }


        $shift = array_shift($goodsList);

        if (count($shift) != $limit) {
            $goodsIds = $this->recommendNewService->getHomeVogueData($userId, array(), 100);
            $goodsList = $this->vogueModel->getVogueGoodsByIds($goodsIds, $wid);
            $goodsList = $this->goodsRepository->filterGoodsNumber($goodsList);
            shuffle($goodsList);
            $goodsList = array_chunk($goodsList, $limit);
            $shift = array_shift($goodsList);
        }

        $this->cacheData($cacheName, $goodsList, 3600);

        return $shift;
    }

    /**
     * @author Jerry
     * @param int $orientation
     * @return bool|int
     */
    protected function getVogueLimit($orientation = 0)
    {

        switch ($orientation) {
            case 1 :
                return 9;
                break;
            case 2:
                return 6;
                break;

            default:
                return false;
                break;
        }
    }

    /**
     * @author Jerry
     * @param array $vogue
     * @return bool|string
     */
    protected function getCategoryIds($vogue = array())
    {
        if (empty($vogue)) {
            return false;
        }

        $vogueIds = array();
        foreach ($vogue as $key => $value) {
            $vogueIds[] = $value['category_id'];
        }

        return implode(',', $vogueIds);
    }
}